#include <iostream>
#include <string>
#include <map>
#include <set>
using namespace std;

class Solution
{
public:
    bool wordPattern(string pattern, string s)
    {
        map<char, string> m;
        set<string> newWords;
        size_t index = -1;
        bool flag = false;
        for (auto c : pattern)
        {
            if (flag)
                return false;
            size_t end = s.find_first_of(' ', index + 1);
            if (end == -1)
            {
                flag = true;
                end = s.length();
            }
            string new_word = s.substr(index + 1, end - index - 1);
            index = end;
            if (m.count(c))
            {
                if (m[c] != new_word)
                    return false;
            }
            else
            {
                if(newWords.count(new_word))
                    return false;
                m[c] = new_word;
                newWords.insert(new_word);
            }
            // cout << new_word << endl;
        }
        return index==s.length();
    }
};

int main()
{
    cout << "Code290" << endl;
    Solution s;
    cout << boolalpha << s.wordPattern("aaa", "dog dog dog dog") << endl;
    return 0;
}